package com.teamfractal.fracdustry.client.screen.Generators;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import com.teamfractal.fracdustry.common.Fracdustry;
import com.teamfractal.fracdustry.common.container.Generators.FDMicrowaveGeneratorContainer;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.FormattedCharSequence;
import net.minecraft.world.entity.player.Inventory;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;

public class FDMicrowaveGeneratorScreen extends AbstractContainerScreen<FDMicrowaveGeneratorContainer> {
    private final ResourceLocation BACKGROUND = new ResourceLocation(Fracdustry.MODID, "textures/gui/microwave_generator/screen.png");
    private final ResourceLocation BATTERY = new ResourceLocation(Fracdustry.MODID, "textures/gui/microwave_generator/battery.png");
    private final ResourceLocation MICROWAVE = new ResourceLocation(Fracdustry.MODID, "textures/gui/microwave_generator/mws.png");

    public FDMicrowaveGeneratorScreen(FDMicrowaveGeneratorContainer container, Inventory inv, Component name) {
        super(container, inv, name);
    }

    @Override
    public void render(@Nonnull PoseStack matrixStack, int mouseX, int mouseY, float partialTicks) {
        this.renderBackground(matrixStack);
        super.render(matrixStack, mouseX, mouseY, partialTicks);
        this.renderTooltip(matrixStack, mouseX, mouseY);
    }

    //render labels
    @Override
    protected void renderLabels(@Nonnull PoseStack matrixStack, int mouseX, int mouseY) {
        int relX = (this.width - 180) / 2;
        int relY = (this.height - 152) / 2;
        List<FormattedCharSequence> tooltip1 = new ArrayList<>();
        //Energy tooltip display
        if(mouseX > relX + 31 && mouseX < relX + 164 && mouseY > relY + 53 && mouseY < relY + 61){
            tooltip1.add(new TextComponent(new TranslatableComponent("gui.fracdustry.energy").getString()+
                    menu.getCurrentEnergy().get(0)+
                    " FE").getVisualOrderText());
            renderTooltip(matrixStack,tooltip1,mouseX - relX,mouseY - relY);}
    }

    protected void renderBg(@Nonnull PoseStack matrixStack, float partialTicks, int mouseX, int mouseY) {
        //Background
        RenderSystem.setShaderTexture(0, BACKGROUND);
        int relX = (this.width - 180) / 2;
        int relY = (this.height - 152) / 2;
        blit(matrixStack, relX, relY, 0, 0, 180, 152, 180, 152);
        //Battery
        RenderSystem.setShaderTexture(0,BATTERY);
        double ce = menu.getCurrentEnergy().get(0);
        double me = menu.getMaxEnergy().get(0);
        int length = Math.toIntExact(Math.round((ce / me) * 132));
        blit(matrixStack, relX + 32, relY + 54, 0, 0, length,
                7,132,7);
        //Microwave
        double time = menu.getTime();
        int p = ((int) (Math.random() * time)) % 5;
        RenderSystem.setShaderTexture(0,MICROWAVE);
        if(menu.getPowered())
        {blit(matrixStack,relX + 13 + p * 33,relY +2,0,0,22,32,22,32);}
        this.font.draw(matrixStack, new TranslatableComponent("gui.fracdustry.title.microwave_generator").getString() , relX + 4, relY + 4, -12829636);
    }
}
